"use strict";
cc._RF.push(module, '3eca8N5/3pDhaqdtgN0KWt5', 'userService');
// scripts/engines/services/userService.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BARDGEANITYPE = exports.MEMBERTYPE = void 0;
var superService_1 = require("./superService");
var gameEngine_1 = require("../gameEngine");
var gameConfig_1 = require("../configs/gameConfig");
var data_control_1 = require("../libs/data_control");
var MEMBERTYPE;
(function (MEMBERTYPE) {
    //钻石
    MEMBERTYPE[MEMBERTYPE["DIAMOND"] = 0] = "DIAMOND";
    //王者
    MEMBERTYPE[MEMBERTYPE["KING"] = 1] = "KING";
})(MEMBERTYPE = exports.MEMBERTYPE || (exports.MEMBERTYPE = {}));
var BARDGEANITYPE;
(function (BARDGEANITYPE) {
    //钻石待机
    BARDGEANITYPE["DIAMONDSTAND"] = "DiamondStand";
    //钻石打开
    BARDGEANITYPE["DIAMONDOPEN"] = "DiamondArise";
    //王者待机
    BARDGEANITYPE["KINGSTAND"] = "KingStand";
    //王者打开
    BARDGEANITYPE["KINGOPEN"] = "Switch";
})(BARDGEANITYPE = exports.BARDGEANITYPE || (exports.BARDGEANITYPE = {}));
var userService = /** @class */ (function (_super) {
    __extends(userService, _super);
    function userService() {
        var _this = _super.call(this) || this;
        _this.className = "用户服务";
        _this.unlock_all_level_flag = false;
        // 解锁过的模式
        _this.m_unlockModes = [];
        _this.m_modeAdCount = {};
        //
        _this.COINS_KEY = "user-coins";
        _this.POWER_KEY = "user-power";
        _this.UNLOCK_KEY = "user-unlock";
        _this.POWER_TIME_KEY = "user-power-time";
        _this.FIRST_PLAY_KEY = "user-first-play";
        _this.UNLOCK_MODE = "user-unlock-mode";
        _this.MODE_ADCOUNT_KEY = "user-mode-adCount";
        _this.LAST_LOGIN_DATE_KEY = "user-last-login-date";
        _this.InfinityModel = 'user-is-infinity';
        _this.InfinityPower_Time_Key = 'user-logintime';
        _this.Level_UnlockAll_key = "user-level-unlock-all-key";
        _this.Level_UnlockAll_Time_Key = "user-level-unlock-all-time-key";
        /**第一次登陆的key */
        _this.first_Login_key = "user-first-login-key";
        /**额外送一个免广告币的key */
        _this.extra_ad_key = "user-extra-ad-key";
        //
        _this.m_currLoginCode = null;
        //
        _this.m_UniqueId = null;
        //
        _this.m_curWebUserInfo = null;
        return _this;
    }
    userService.prototype.loadService = function () {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, _super.prototype.loadService.call(this)];
                    case 1:
                        _a.sent();
                        this.onInitNormalData(gameConfig_1.default.MAX_POWER, gameConfig_1.default.NORMAL_POWER, gameConfig_1.default.NORMAL_COINS, gameConfig_1.default.ADD_ONCE_POWER, gameConfig_1.default.REST_POWER_TIME, gameConfig_1.default.REST_POWER_VALUE, gameConfig_1.default.ADD_ONCE_COINS);
                        //
                        // this.onWebInfoUpdate();
                        return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    /**
     * 是否在新手保护的时间内
     * @returns
     */
    userService.prototype.isFirstPlayerTimeIn = function () {
        return (Date.now() - this.m_firstPlayerTime < gameConfig_1.default.FIRST_VIDEO_TIME * 1000);
    };
    userService.prototype.onInitNormalData = function (maxPower, normalPower, normalCoins, addOncePower, restPowerTime, restPowerValue, addOnceCoins) {
        this.m_powerMax = maxPower;
        this.m_normalPower = normalPower;
        this.m_normalCoins = normalCoins;
        this.m_constAddOncePower = addOncePower;
        this.m_constResetPowerTime = restPowerTime;
        this.m_constResetPowerValue = restPowerValue;
        this.m_constAddOnceCoins = addOnceCoins;
        //
        this.loadUserData();
    };
    userService.prototype.getFirstLogin = function () {
        return gameEngine_1.default.getServices().localStorageSrv.getStorage(this.first_Login_key);
    };
    userService.prototype.setFirstLogin = function (num) {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.first_Login_key, num);
    };
    userService.prototype.getExtraAd = function () {
        return gameEngine_1.default.getServices().localStorageSrv.getStorage(this.extra_ad_key);
    };
    userService.prototype.setExtraAd = function (num) {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.extra_ad_key, num);
    };
    userService.prototype.getResetPowerTime = function () {
        return this.m_constResetPowerTime;
    };
    userService.prototype.getResetPowerValue = function () {
        return this.m_constResetPowerValue;
    };
    userService.prototype.getInfinityPower = function () {
        return gameEngine_1.default.getServices().localStorageSrv.getStorage(this.InfinityModel) == "1";
    };
    userService.prototype.setInfinity = function (infinityNum) {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.InfinityModel, infinityNum);
    };
    userService.prototype.getInfinityTime = function () {
        return gameEngine_1.default.getServices().localStorageSrv.getStorage(this.InfinityPower_Time_Key);
    };
    userService.prototype.setInfinityTime = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.InfinityPower_Time_Key, data_control_1.default.getCurrDateDayString());
    };
    userService.prototype.loadLevelAllUnlockFlag = function () {
        var str = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.Level_UnlockAll_key) || "false";
        this.unlock_all_level_flag = JSON.parse(str);
        this.writeLevelAllUnlockFlag();
    };
    userService.prototype.getLevelAllUnlockFlag = function () {
        return this.unlock_all_level_flag;
    };
    userService.prototype.setLevelAllUnlockFlag = function (flag) {
        this.unlock_all_level_flag = flag;
        this.writeLevelAllUnlockFlag();
    };
    userService.prototype.writeLevelAllUnlockFlag = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.Level_UnlockAll_key, this.unlock_all_level_flag);
    };
    userService.prototype.getLevelAllUnlockTime = function () {
        var str = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.Level_UnlockAll_Time_Key) || "0";
        var time = JSON.parse(str);
        if (isNaN(time)) {
            time = 0;
        }
        return time;
    };
    userService.prototype.setLevelAllUnlockTime = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.Level_UnlockAll_Time_Key, Date.now());
    };
    userService.prototype.setCoins = function (coins, isPlus) {
        if (isPlus) {
            coins = this.m_coins + coins;
            if (coins > 0) {
                gameEngine_1.default.getManagers().soundMgr.playSound("mainSound/GetCoin");
            }
        }
        this.m_coins = data_control_1.default.getValueOfAreaMinMax(coins, 0, Number.MAX_VALUE);
        this.writeCoins();
    };
    userService.prototype.getCoins = function () {
        return this.m_coins;
    };
    userService.prototype.setPower = function (power, isPlus, isCanBeyond, isReset) {
        if (isReset === void 0) { isReset = false; }
        if (isPlus) {
            power = this.m_power + power;
        }
        //满的开始扣
        if (this.m_powerMax == this.m_power && power == this.m_power - 1) {
            isReset = true;
        }
        if (isCanBeyond) {
            this.m_power = data_control_1.default.getValueOfAreaMinMax(power, 0, 9999);
        }
        else {
            // if (this.m_power < this.m_powerMax) {
            this.m_power = data_control_1.default.getValueOfAreaMinMax(power, 0, this.m_powerMax);
            // }
        }
        this.writePower(isReset);
    };
    userService.prototype.getMaxPower = function () {
        return this.m_powerMax;
    };
    userService.prototype.getNormalPower = function () {
        return this.m_normalPower;
    };
    userService.prototype.getPower = function () {
        if (isNaN(Number(this.m_power))) {
            console.info("获取体力异常归零", this.m_power);
            gameEngine_1.default.getServices().localStorageSrv.setStorage(this.POWER_KEY, 0);
            this.m_power = 0;
        }
        return this.m_power;
    };
    userService.prototype.getMemberStatus = function () {
        var ms = gameEngine_1.default.getServices().localStorageSrv.getStorage("private-member-type") ||
            "0";
        return Number(ms);
    };
    userService.prototype.setMemberStatus = function (type) {
        if (type === void 0) { type = 0; }
        gameEngine_1.default.getServices().localStorageSrv.setStorage("private-member-type", type);
        gameEngine_1.default.getServices().localStorageSrv.setStorage("user-membertime", Date.now());
    };
    //看广告添加体力
    userService.prototype.addPowerOfVideo = function () {
        //
        this.setPower(this.m_constAddOncePower, true, true);
    };
    userService.prototype.getConstAddOncePower = function () {
        return this.m_constAddOncePower;
    };
    userService.prototype.setUnlockLevel = function (unlockLevel, isPlus) {
        if (isPlus) {
            unlockLevel = this.m_unlockLevel + unlockLevel;
        }
        this.m_unlockLevel = unlockLevel;
        this.writeUnLockLevel();
    };
    userService.prototype.getUnlockLevel = function () {
        return this.m_unlockLevel;
    };
    //回复体力倒计时
    userService.prototype.getPowerResetTimer = function () {
        var dateNow = Date.now();
        var dateOld = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.POWER_TIME_KEY) ||
            dateNow;
        return (this.m_constResetPowerTime - Math.floor((dateNow - Number(dateOld)) / 1e3));
    };
    userService.prototype.onPowerTimerReset = function () {
        //计算体力恢复
        var dateNow = Date.now();
        var dateOld = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.POWER_TIME_KEY) ||
            dateNow;
        dateOld = Number(dateOld);
        var restCount = Math.floor((dateNow - dateOld) / (this.m_constResetPowerTime * 1000));
        //
        var addHp = restCount * this.m_constResetPowerValue;
        if (addHp && this.getPower() < this.m_powerMax) {
            this.setPower(addHp, true, false, true);
            return true;
        }
        return false;
    };
    userService.prototype.UnlockMode = function (mode) {
        if (!this.getModeUnlocked(mode)) {
            this.m_unlockModes.push(mode);
            this.writeUnlockMode();
        }
    };
    userService.prototype.getModeUnlocked = function (mode) {
        return (this.m_unlockModes.includes(mode) ||
            gameConfig_1.default.MODE_VIDEO_COUNT[mode] == 0);
    };
    userService.prototype.addModeAdCount = function (mode) {
        this.m_modeAdCount[mode]++;
        this.writeModeAdCount();
    };
    userService.prototype.getModeAdCount = function (mode) {
        return this.m_modeAdCount[mode];
    };
    userService.prototype.loadUser = function () {
        //
        this.loadNameAndHead();
    };
    userService.prototype.loadFirstPlayerTime = function () {
        this.m_firstPlayerTime = Number(gameEngine_1.default.getServices().localStorageSrv.getStorage(this.FIRST_PLAY_KEY) || "0");
        if (this.m_firstPlayerTime == 0) {
            this.m_firstPlayerTime = Date.now();
            this.writeFirstPlayerTime();
        }
    };
    userService.prototype.writeFirstPlayerTime = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.FIRST_PLAY_KEY, this.m_firstPlayerTime);
    };
    userService.prototype.loadUserData = function () {
        this.loadCoins();
        this.loadPower();
        this.loadUnlockLevel();
        this.loadFirstPlayerTime();
        this.loadUnlockMode();
        this.loadModeAdCount();
        //
        this.loadNameAndHead();
        this.loadLevelAllUnlockFlag();
    };
    userService.prototype.setLastLoginDate = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.LAST_LOGIN_DATE_KEY, data_control_1.default.getCurrDateDayString());
    };
    userService.prototype.getLastLoginDate = function () {
        return gameEngine_1.default.getServices().localStorageSrv.getStorage(this.LAST_LOGIN_DATE_KEY);
    };
    userService.prototype.loadModeAdCount = function () {
        this.m_modeAdCount = {
            0: 0,
            1: 0,
            3: 0,
            4: 0,
            5: 0,
            6: 0,
            7: 0,
            8: 0,
        };
        //TODO 新模式请看这里
        if (gameEngine_1.default.getServices().localStorageSrv.getStorage(this.MODE_ADCOUNT_KEY)) {
            var object = JSON.parse(gameEngine_1.default.getServices().localStorageSrv.getStorage(this.MODE_ADCOUNT_KEY));
            for (var key in object) {
                if (Object.prototype.hasOwnProperty.call(object, key)) {
                    var element = object[key];
                    this.m_modeAdCount[key] = element;
                }
            }
        }
    };
    userService.prototype.writeModeAdCount = function () {
        gameEngine_1.services().localStorageSrv.setStorage(this.MODE_ADCOUNT_KEY, JSON.stringify(this.m_modeAdCount));
    };
    userService.prototype.loadUnlockMode = function () {
        var dataStr = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.UNLOCK_MODE);
        if (dataStr) {
            this.m_unlockModes = JSON.parse(dataStr);
            // console.warn(this.m_unlockModes);
        }
    };
    userService.prototype.writeUnlockMode = function () {
        gameEngine_1.services().localStorageSrv.setStorage(this.UNLOCK_MODE, JSON.stringify(this.m_unlockModes));
    };
    userService.prototype.loadUnlockLevel = function () {
        this.m_unlockLevel = Number(gameEngine_1.default.getServices().localStorageSrv.getStorage(this.UNLOCK_KEY) || 0);
    };
    userService.prototype.writeUnLockLevel = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.UNLOCK_KEY, this.m_unlockLevel);
    };
    userService.prototype.loadCoins = function () {
        this.m_coins =
            Number(gameEngine_1.default.getServices().localStorageSrv.getStorage(this.COINS_KEY) || 0) || this.m_normalCoins;
    };
    userService.prototype.loadPower = function () {
        var powerStr = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.POWER_KEY);
        if (isNaN(Number(powerStr))) {
            console.info("初始异常归零", powerStr);
            gameEngine_1.default.getServices().localStorageSrv.setStorage(this.POWER_KEY, 0);
            powerStr = "0";
        }
        if (powerStr === "" || powerStr === null)
            this.m_power = this.m_normalPower;
        else
            this.m_power = Number(powerStr);
        //体力恢复
        this.onPowerTimerReset();
    };
    userService.prototype.writeCoins = function () {
        //
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.COINS_KEY, this.m_coins);
    };
    userService.prototype.writePower = function (isReset) {
        //
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.POWER_KEY, this.m_power);
        var nil = gameEngine_1.services().localStorageSrv.getStorage(this.POWER_TIME_KEY);
        if (isReset || !nil) {
            var dateNow = Date.now();
            gameEngine_1.default.getServices().localStorageSrv.setStorage(this.POWER_TIME_KEY, dateNow);
        }
    };
    userService.prototype.loadNameAndHead = function () {
        //
        // return;
        //
        var currMiniApp = gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance();
        //
        var fail = function () {
            currMiniApp.showToast({ title: "登录失败" });
        };
        //已登录
        // if (this.m_currLoginCode) return;
        //登录
        currMiniApp.login({
            force: false,
            success: function (res) {
                // this.m_currLoginCode = res.code;
                //授权
                currMiniApp.authorize({
                    success: function () {
                        //信息获取
                        currMiniApp.getUserInfo({
                            success: function (res) {
                                // this.m_name = res.userInfo.nickName;
                                // this.m_headImage = res.userInfo.avatarUrl;
                            },
                            fail: fail,
                        });
                    },
                    fail: fail,
                });
                // //获取唯一ID
                // if (this.m_UniqueId) {
                //     return;
                // }
                // //
                // gameHttp_module.getUserWxIdentifyByCode(this.m_currLoginCode, (res) => {
                //     console.debug('我的唯一ID', res);
                //     this.m_UniqueId = res;
                //     gameHttp_module.getUserWebInfo(this.m_UniqueId, (res) => {
                //         this.m_curWebUserInfo = JSON.parse(res);
                //         console.debug('读取到的数据', res);
                //         this.onWebInfoGetOnce();
                //     }, res1 => {
                //         this.m_curWebUserInfo = {
                //             m_coins: this.m_normalCoins,
                //             m_devilCurrLevel: 0,
                //             m_synthesisEquipmentList: [],
                //             m_unlockStamp: 0,
                //         }
                //     });
                // })
            },
            fail: fail,
        });
    };
    userService.prototype.onWebInfoUpdate = function () {
        var _this = this;
        setInterval(function () {
            if (_this.m_UniqueId == null || _this.m_curWebUserInfo == null)
                return;
            for (var key in _this.m_curWebUserInfo) {
                _this.m_curWebUserInfo[key] = _this[key];
            }
            console.log("更新数据", _this.m_UniqueId, _this.m_curWebUserInfo);
        }, 3e3);
    };
    userService.prototype.onWebInfoGetOnce = function () {
        //没有线上
        if (this.m_UniqueId == null || this.m_curWebUserInfo == null)
            return;
        //如果经验金币都有，就说明读到了
        if (this.m_coins != this.m_normalCoins)
            return;
        console.log("获取数据", this.m_curWebUserInfo);
        //
        for (var key in this.m_curWebUserInfo) {
            this[key] = this.m_curWebUserInfo[key];
        }
    };
    return userService;
}(superService_1.default));
exports.default = userService;

cc._RF.pop();